﻿using System;
using BLL;
using DAL;
using Ext.Net;
using System.Data;
using Roles;

namespace ERP.Modules
{
    public partial class TypeContract : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flag;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetTypeContract();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //Store
        protected void StoreTypeContract_RefreshData(object sender, StoreReadDataEventArgs e)
        {

        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            ClearText();
            WindowsTypeContract.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grTypeContract.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    ClearText();
                    GettypeContractEdit(int.Parse(row.RecordID));
                    WindowsTypeContract.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grTypeContract.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtTypeContract { TypeContract_ID = int.Parse(row.RecordID) };
                        BllTypeContract.Delete(obj);
                        if (obj.Msg == 0)
                            HTML.clsFormat.Alert_ERROR("Lỗi", "Loại hợp đồng đang được sử dụng,không thể xoá !");
                    }
                    catch (Exception ex)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                    }
                }
            GetTypeContract();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsTypeContract.Close();
        }
        //Double-Click
        protected void TypeContractDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            ClearText();
            int TypeContract_ID = int.Parse(e.ExtraParams["TypeContract_ID"]);
            GettypeContractEdit(TypeContract_ID);
            WindowsTypeContract.Show();
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsGetTypeContract = new BllTypeContract();
            var _dtGetTypeContract = _clsGetTypeContract.GetTypeContract(txtSearch.Text);
            if (_dtGetTypeContract != null && _dtGetTypeContract.Rows.Count > 0)
            {
                StoreTypeContract.DataSource = _dtGetTypeContract;
                StoreTypeContract.DataBind();
            }
            else
            {
                grTypeContract.GetStore().RemoveAll();
            }
        }
        #endregion

        #region[Method]
        private void GetTypeContract()
        {
            var _clsGetTypeContract = new BllTypeContract();
            var _dtGetTypeContract = _clsGetTypeContract.GetTypeContract(string.Empty);
            if (_dtGetTypeContract != null && _dtGetTypeContract.Rows.Count > 0)
            {
                StoreTypeContract.DataSource = _dtGetTypeContract;
                StoreTypeContract.DataBind();
            }
            else
            {
                grTypeContract.GetStore().RemoveAll();
            }
        }
        private void GettypeContractEdit(int id)
        {
            var _clsGetTypeContractEdit = new BllTypeContract();
            var _dtGetTypeContractEdit = _clsGetTypeContractEdit.GetTypeContractEdit(id);
            if (_dtGetTypeContractEdit != null && _dtGetTypeContractEdit.Rows.Count > 0)
            {
                txtTypeContract_ID.Text = _dtGetTypeContractEdit.Rows[0]["TypeContract_ID"].ToString();
                txtTypeContract_Code.Text = _dtGetTypeContractEdit.Rows[0]["TypeContract_Code"].ToString();
                txtTypeContract_Name.Text = _dtGetTypeContractEdit.Rows[0]["TypeContract_Name"].ToString();
                txtTypeContract_Note.Text = _dtGetTypeContractEdit.Rows[0]["TypeContract_Node"].ToString();
            }
        }
        private bool CheckInsert()
        {
            bool kiemtra = false;
            var _clsGetTypeContract = new BllTypeContract();
            var _dtTypeContract = _clsGetTypeContract.GetTypeContract(string.Empty);
            if (_dtTypeContract != null && _dtTypeContract.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtTypeContract.Rows)
                {
                    if (txtTypeContract_ID.Text != dr["TypeContract_ID"].ToString())
                    {
                        if (txtTypeContract_Code.Text == dr["TypeContract_Code"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã loại hợp đồng này đã tồn tại !");
                        }
                        else if (txtTypeContract_Name.Text == dr["TypeContract_Name"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Tên loại hợp đồng này đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtTypeContract
                    {
                        TypeContract_Code = txtTypeContract_Code.Text,
                        TypeContract_Name = txtTypeContract_Name.Text,
                        TypeContract_Node = txtTypeContract_Note.Text,
                    };
                    BllTypeContract.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    ClearText();
                    GetTypeContract();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtTypeContract
                    {
                        TypeContract_ID = int.Parse(txtTypeContract_ID.Text),
                        TypeContract_Code = txtTypeContract_Code.Text,
                        TypeContract_Name = txtTypeContract_Name.Text,
                        TypeContract_Node = txtTypeContract_Note.Text,
                    };
                    BllTypeContract.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                    GetTypeContract();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[ClearText]
        private void ClearText()
        {
            FormPanelTypeContract.Reset();
            txtTypeContract_ID.Text = string.Empty;
            txtTypeContract_Code.Text = AutoCode();
        }
        #endregion

        #region[AutoCode]
        private string AutoCode()
        {
            int i;
            var _clsGetTypeContract = new BllTypeContract();
            var dt = _clsGetTypeContract.GetTypeContract(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["TypeContract_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["TypeContract_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["TypeContract_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "LHD-00" + i;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.TypeContract, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelTypeContract.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            bool check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.TypeContract, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}